/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.ASWingUtils;
import org.aswing.border.EmptyBorder;
import org.aswing.BorderLayout;
import org.aswing.geom.Point;
import org.aswing.Insets;
import org.aswing.JFrame;
import org.aswing.JLabel;
import org.aswing.JPanel;
import org.aswing.JScrollPane;

import com.bourre.commands.Delegate;
import com.bourre.data.collections.Map;
import com.bourre.data.iterator.Iterator;

import flash.display.BitmapData;

import fvaswing.components.filechooser.events.FvFileChooserCommander;
import fvaswing.components.filechooser.events.FvFileChooserEventList;
import fvaswing.components.filechooser.mvc.FvFileChooserView;
import fvaswing.components.filechooser.preview.FvChooserPreview;
import fvaswing.components.FvBitmapIcon;
import fvaswing.visual.FvViewHelper;
import fever.data.file.File;

/**
 * {@code FvImagePopup } class.
 *
 * @author Romain Ecarnot
 */
class fvaswing.components.filechooser.preview.FvImagePopup
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _map : Map = new Map();
	
	private var _frame : JFrame;
	private var _currentItem : File;
	private var _image : JLabel;
	private var _informations : JLabel;
	private var _scroll : JScrollPane;
	
	private var _command : Delegate;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	public static function open( item : File ) : Void
	{
		var target : MovieClip = FvChooserPreview( FvViewHelper.getViewHelper( FvFileChooserView.PREVIEW ) ).getChooserMovieTarget();
		
		var id : String = item.getPath();
		var popup : FvImagePopup;
		
		if( !_map.containsKey( id ) )
		{
			popup = new FvImagePopup( target );
			popup.load( item );
			_map.put( id, popup );
		}
		else
		{
			popup = _map.get( id );
			popup._frame.toFront();
		}
	}
	
	public static function remove( id : String ) : Void
	{
		var popup : FvImagePopup = _map.get( id );
		_map.remove( id );
		
		popup.release();
		delete popup;
	}
	
	public static function removeAll() : Void
	{
		var it : Iterator = _map.getKeysIterator();
		while( it.hasNext() )
		{
			FvImagePopup.remove( it.next() );	
		}
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function FvImagePopup( target : MovieClip )
	{
		_command = new Delegate( this, _handleClose );
		FvFileChooserCommander.getInstance()._oEB.addEventListener( FvFileChooserEventList.CLOSE_CHOOSER, _command );
		
		_configureUI( target );
	}
	
	/**
	 * Loads a {@link fever.data.file.File}
	 */
	private function load( item : File ) : Void
	{
		if( item != _currentItem )
		{
			var bmp : BitmapData = BitmapData( item.getContent() ).clone();
			
			var size : Number = item.getSize();
			var txSize : String = ( ( size > 1024 ) ? Math.round( size / 1024 ) : 1 ) + " K";
			
			_informations.setText( bmp.width + 'x' + bmp.height + ' | ' + txSize );
			_image.setIcon( new FvBitmapIcon( bmp, bmp.width, bmp.height ) );
			_scroll.revalidateIfNecessary();
			
			_frame.setTitle( item.getName() );
			
			var location : Point = ASWingUtils.getScreenCenterPosition();
			location.x -= _frame.getWidth()/2;
			location.y -= _frame.getHeight()/2;
			_frame.setLocation( location );
			_frame.show();
			
			_currentItem = item;
		}
	}
	
	private function _configureUI( target : MovieClip ) : Void
	{
		_image = new JLabel();
		_scroll = new JScrollPane( 
			_image, 
			JScrollPane.SCROLLBAR_AS_NEEDED,
			JScrollPane.SCROLLBAR_AS_NEEDED
		);
		
		_informations = new JLabel( '', JLabel.LEFT );
		
		var panel : JPanel = new JPanel( new BorderLayout( 5, 5 ) );
		panel.append( _scroll, BorderLayout.CENTER );
		panel.append( _informations, BorderLayout.SOUTH );
		
		_frame = new JFrame( target, '', false );
		_frame.setResizable( true );
		_frame.setClosable( true );
		_frame.setSize( 300, 200 );
		_frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );		_frame.addEventListener( JFrame.ON_WINDOW_CLOSING, _handleClose, this );
		
		_frame.getContentPane().setBorder( new EmptyBorder( null, new Insets( 5, 5, 5, 5 ) ) );
		_frame.getContentPane().setLayout( new BorderLayout( 4, 4 ) );
		_frame.setContentPane( panel );	
	}
	
	private function release() : Void
	{
		_currentItem = null;
		_image.destroy();
		_informations.destroy();
		_scroll.destroy();
		_frame.destroy();
		_frame.tryToClose();
	}
	
	private function _handleClose() : Void
	{
		FvFileChooserCommander.getInstance()._oEB.removeEventListener( FvFileChooserEventList.CLOSE_CHOOSER, _command );
		_frame.tryToClose();
		remove( _currentItem.getPath() );
	}
}